package cn.iocoder.yudao.module.system.dal.mysql.merchant;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 梁富贵
 * @Date 2024/10/9
 **/
@Mapper
public interface MerchantMapper extends BaseMapperX<MerchantDO> {

    default PageResult<MerchantDO> selectPage(MerchantReqVO reqVO) {
        LambdaQueryWrapperX<MerchantDO> query = new LambdaQueryWrapperX<>();
        if (!Objects.isNull(reqVO.getCreateTime()) && reqVO.getCreateTime().length == 2) {
            query.between(MerchantDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        return selectPage(reqVO, query.likeIfPresent(MerchantDO::getMerchantName, reqVO.getMerchantName())
                .likeIfPresent(MerchantDO::getContactPhone, reqVO.getContactPhone())
                .eqIfPresent(MerchantDO::getMerchantIdentity, reqVO.getMerchantIdentity())
                .eqIfPresent(MerchantDO::getStatus, reqVO.getStatus())
                .likeIfPresent(MerchantDO::getLegalRepresentative, reqVO.getLegalRepresentative()));
    }

    default List<MerchantDO> selectList(List<Long> lists) {
        return selectList(new LambdaQueryWrapperX<MerchantDO>()
                .inIfPresent(MerchantDO::getMerchantId, lists));
    }

    default CommonResult<MerchantDO> getStoreProfile(long id) {
        // 使用 selectById 方法通过 id 查询商家信息
        MerchantDO merchant = selectById(id); // 假设这是合法调用，实际代码中静态方法不能访问实例方法，你需要将其移到调用层处理。

        // 如果商家信息不存在，返回错误信息
        if (merchant == null) {
            return null;
        }

        // 返回商家信息
        return CommonResult.success(merchant);
    }


    default MerchantDO selectByUserId(long userId) {
        // 使用 LambdaQueryWrapper 构建查询条件，查询 userId 对应的商家信息
        return selectOne(new LambdaQueryWrapperX<MerchantDO>()
                .eq(MerchantDO::getMerchantId, userId));
    }

    default void deleteByUserId(MerchantDO merchant) {
        LambdaQueryWrapper<MerchantDO> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(MerchantDO::getMerchantId, merchant.getMerchantId());

        this.delete(deleteWrapper);


    }

    default void updateByUserId(MerchantDO merchant) {
        LambdaUpdateWrapper<MerchantDO> updateWrapper = new LambdaUpdateWrapper<>();

        // 设置更新条件：根据 userId 更新
        updateWrapper.eq(MerchantDO::getMerchantId, merchant.getMerchantId());

        // 更新内容：如果字段不为 null，则进行更新
        updateWrapper.set(merchant.getMerchantName() != null, MerchantDO::getMerchantName, merchant.getMerchantName())
                .set(merchant.getMainProduct() != null, MerchantDO::getMainProduct, merchant.getMainProduct())
                .set(merchant.getDescription() != null, MerchantDO::getDescription, merchant.getDescription())
                .set(merchant.getContactAddress() != null, MerchantDO::getContactAddress, merchant.getContactAddress())
                .set(merchant.getContactEmail() != null, MerchantDO::getContactEmail, merchant.getContactEmail())
                .set(merchant.getContactPhone() != null, MerchantDO::getContactPhone, merchant.getContactPhone())
                .set(merchant.getMerchantIdentity() != null, MerchantDO::getMerchantIdentity, merchant.getMerchantIdentity());

        // 执行更新
        update(merchant, updateWrapper);
    }

    /**
     * 根据用户 ID 列表，获得用户 Map
     *
     * @param userIds 用户编号列表
     * @return 用户 Map
     */
    default List<MerchantDO> selectListByUserIds(List<Long> userIds) {
        if (Objects.isNull(userIds) || userIds.isEmpty()) {
            return null;
        }
        return selectList(new LambdaQueryWrapper<MerchantDO>().in(MerchantDO::getMerchantId, userIds));
    }

    default Collection<MerchantDO> selectByMerchantIds(Set<Long> merchantIds) {
        if (Objects.isNull(merchantIds) || merchantIds.isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<MerchantDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MerchantDO::getMerchantId, merchantIds);

        // 执行查询并返回结果
        return selectList(queryWrapper);
    }

}
